package q1.project.log;

import java.time.LocalDateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * 操作日志数据访问接口
 *
 * @author Q1 Team
 * @version 1.0
 */
@Repository
public interface OperationLogDao extends JpaRepository<OperationLogEntity, String> {

  /** 根据租户ID分页查询 */
  Page<OperationLogEntity> findByTenantIdOrderByCreateTimeDesc(String tenantId, Pageable pageable);

  /** 根据用户ID分页查询 */
  Page<OperationLogEntity> findByUserIdOrderByCreateTimeDesc(String userId, Pageable pageable);

  /** 根据模块分页查询 */
  Page<OperationLogEntity> findByModuleOrderByCreateTimeDesc(String module, Pageable pageable);

  /** 删除指定时间之前的日志 */
  @Modifying
  @Query("DELETE FROM OperationLogEntity o WHERE o.createTime < :beforeTime")
  void deleteByCreateTimeBefore(@Param("beforeTime") LocalDateTime beforeTime);
}
